import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

n_values = [10, 40, 160]
plt.figure(figsize=(10, 8))

for n in n_values:
    try:
        data = pd.read_csv(f'data_m{n}.csv', header=None)
        plt.plot(data.to_numpy()[:, 0], data.to_numpy()[:, 1], label=f'm={n}', marker='o')  # 转换为 NumPy 数组
    except FileNotFoundError:
        print(f"File data_m{n}.csv not found.")
    except Exception as e:
        print(f"An error occurred while processing data_m{n}.csv: {e}")

# 绘制精确函数
t = np.linspace(0, 2 * np.pi, 400)
x_exact = 16 * np.sin(t) ** 3
y_exact = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
plt.plot(x_exact, y_exact, 'k--', label='Exact function')

# 添加图例和标签
plt.legend()
plt.title('Heart')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)

# 保存图像
plt.savefig('heart.png')

plt.show()
